#pragma once
#include "Protocol.hpp"

class Cal
{
public:
    Cal() = default;
    ~Cal() = default;

    Response Calculate(const Request &req) const
    {
        int x = req.X();
        int y = req.Y();
        char oper = req.Oper();
        int result, code;
        result = code = 0;
        switch (oper)
        {
        case '+':
        {
            result = x + y;
            break;
        }
        case '-':
        {
            result = x - y;
            break;
        }
        case '*':
        {
            result = x * y;
            break;
        }
        case '/':
        {
            if (y == 0)
                code = 1; // 1表示除0异常
            else
                result = x / y;
            break;
        }
        case '%':
        {
            if (y == 0)
                code = 2; // 2表示mod 0异常
            else
                result = x % y;
            break;
        }
        default:
        {
            code = 3; // 3表示操作符错误
            break;
        }
        }
        Response resp;
        resp.SetResponse(result, code);
        return resp;
    }
};